package com.fxc;

import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.fxc.tank.abstractfactory.BaseBullet;
import com.fxc.tank.abstractfactory.BaseExplode;
import com.fxc.tank.abstractfactory.BaseTank;
import com.fxc.tank.abstractfactory.DefaultFactory;
import com.fxc.tank.abstractfactory.GameFactory;
import com.fxc.tank.abstractfactory.RectFactory;

public class TankFrame extends Frame {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public static final int GMAE_WIDTH = Integer.parseInt((String)PropertyManager.get("gameWidth")), 
			GAME_HEIGHT = Integer.parseInt((String)PropertyManager.get("gameHeight"));
	
	public List<BaseBullet> bullets = new ArrayList<>();
	
	public List<Tank> tanks = new ArrayList<>();
	
	public List<BaseExplode> explods = new ArrayList<>();	
	
	Tank tank = new Tank(200, 400, Dir.DOWN, this,Group.GOOD);
	
	Bullet bullet = new Bullet(300, 300, Dir.DOWN, this, Group.BAD);
	
	// Explods explods = new Explods(100, 100, this);
	public GameFactory gf = new RectFactory();
	

	public TankFrame() {
		setSize(GMAE_WIDTH,GAME_HEIGHT);
		setResizable(false);
		setTitle("̹坦克大战");
		setVisible(true);
		setBackground(Color.black);
		
		addWindowListener(new WindowAdapter() { 

			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
			
		});
		
		this.addKeyListener(new mykeyListener());
		new Audio().start();
		
	}
	

	
	
	/**
	 * 双缓冲技术，解决屏幕闪烁问题
	 * @param g
	 */
	Image offScreenImage = null;
	
	@Override
	public void update(Graphics g) {
		if (offScreenImage == null) {
			offScreenImage = this.createImage(GMAE_WIDTH, GAME_HEIGHT);
		}
		
		Graphics gOffGraphics = offScreenImage.getGraphics();
		
		Color color = gOffGraphics.getColor();
		
		gOffGraphics.setColor(Color.BLACK);
		
		gOffGraphics.fillRect(0, 0, GMAE_WIDTH, GAME_HEIGHT);
		
		gOffGraphics.setColor(color);
		
		paint(gOffGraphics);
		
		g.drawImage(offScreenImage, 0, 0, null);
		
	}



	@Override
	public void paint(Graphics g) {
		
		//设置子弹数量
		Color color = g.getColor();
		g.setColor(Color.WHITE);
		g.drawString("子弹的数量："+ bullets.size(), 10, 60);
		g.drawString("敌人坦克的数量："+ tanks.size(), 10, 80);
		g.drawString("爆炸的数量："+ explods.size(), 10, 100);
		g.setColor(color);
		
		tank.paint(g);
		
		for (int i = 0; i < bullets.size(); i++) {
			bullets.get(i).paint(g);
		}
		
		for (int i = 0; i < tanks.size(); i++) {
			tanks.get(i).paint(g);
		}
		
		for (int i = 0; i < explods.size(); i++) {
			explods.get(i).paint(g);
		}
		
		// collision detect
		for (int i = 0; i < bullets.size(); i++) {
			for (int j = 0; j < tanks.size(); j++) {
				bullets.get(i).collideWIth(tanks.get(j));
			}
		}
		

		
		
		//explods.paint(g);
//		for (Bullet bullet : bullets) {
//			bullet.paint(g);
//		}
		// bullet.paint(g);
//		for(Iterator<Bullet> it = bullets.iterator();it.hasNext();) {
//			Bullet b = it.next();
//			if(!b.isLive()) {
//				it.remove();
//			}
//		}
		
	}
	
	
	class mykeyListener extends KeyAdapter {
		
		boolean bL = false;
		boolean bU = false;
		boolean bR = false;
		boolean bD = false;

		/**
		 * 按键按下时调用
		 */
		@Override
		public void keyPressed(KeyEvent e) {
			
			// System.out.println(e.getKeyCode());
			// x += 50;
			
			int key = e.getKeyCode();
			
			switch (key) {
			case KeyEvent.VK_LEFT:
				bL = true;
				break;
			case KeyEvent.VK_UP:
				bU = true;
				break;
			case KeyEvent.VK_RIGHT:
				bR = true;
				break;
			case KeyEvent.VK_DOWN:
				bD = true;
				break;

			default:
				break;
			}
			
			setMainTankDir();
		}

		/**
		 * 按键释放时调用
		 */
		@Override
		public void keyReleased(KeyEvent e) {
			int key = e.getKeyCode();
			
			switch (key) {
			case KeyEvent.VK_LEFT:
				bL = false;
				break;
			case KeyEvent.VK_UP:
				bU = false;
				break;
			case KeyEvent.VK_RIGHT:
				bR = false;
				break;
			case KeyEvent.VK_DOWN:
				bD = false;
				break;
			case KeyEvent.VK_CONTROL:
				tank.fire();
				break;
				
			default:
				break;
			}
			
			setMainTankDir();
		}

		private void setMainTankDir() {
			
			if (!bL && !bU && !bR && !bD) {
				
				tank.setMoving(false);
				
			}else {
				tank.setMoving(true);
				
				if (bL) {tank.setDir(Dir.LEFT);}
				if (bU) {tank.setDir(Dir.UP);}
				if (bR) {tank.setDir(Dir.RIGHT);}
				if (bD) {tank.setDir(Dir.DOWN);}
				
				
			}
		}
		
	}
}
